<?php

class ReportController extends \BaseController {

	public $year = '';
	public $last_month = '';
	public $auth_user;

	public function __construct() {
		$this->year = date("Y");
		$this->last_month = date("m");
		$this->auth_user = Auth::user();
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$year  = Input::get('year', $this->year);
		$month =  Input::get('month', $this->last_month);
		$user_id  =  Input::get('user_id', $this->auth_user->id);
		$profile_list = array();
		if($this->auth_user->role == 'admin')
			$profile = User::get();
		elseif($this->auth_user->role == 'manager')
			$profile = User::where('department',$this->auth_user->department)->whereActive(1)->get();
		else
			$profile = array();
		foreach ($profile as $value) {
			$profile_list[] = array('user_id' => $value->id, 'name' => $value->name, 'department' => $value->department);
		}

		$reports = Report::with([
                    'comments' => function($q){
                            return $q->with(
                                array('owner' => function($query){
                                        return $query->get(array('id','name','role'));
                                    }))
                                ->orderBy('created_at', 'desc')
                                ->get();
                        },
                    ])
                    ->with('owner')
                    ->where(DB::raw('YEAR(date)'), $year)
					->where(DB::raw('Month(date)'), $month)
					->where('user_id',$user_id)
					->orderBy('date','desc');
//		$report_list = Report::with('comments')
//                    ->where(DB::raw('YEAR(date)'), $year)
//					->where(DB::raw('Month(date)'), $month)
//                    ->where('user_id',$user_id)
//					->orderBy('date','desc');

        $json = array('report_list' => $reports->get()->toArray(), 'profile_list' => $profile_list, 'year' => $year, 'month' => $month, 'user_id' => $user_id);
        return Response::json($json, 200);

	}



	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$report = new Report;
		$report->user_id = $this->auth_user->id;
        $report->title = Input::get('title');
		$report->date = Input::get('date');
		$report->content = Input::get('content');
		$report->plain = Input::get('plan');
		$report->notes = Input::get('notes', '');
		$report->save();
		return $report;
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$report = Report::find($id);

		$report->delete();

		return '删除成功';
	}

    public function postComment()
    {
        $comment = new Comment;
        $comment->user_id = $this->auth_user->id;
        $comment->report_id = Input::get('id');
        $comment->content = Input::get('content');
        $comment->save();
        return $comment;
    }
}
